//
//  JKCommentModel.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKCommentModel: JKBaseModel {
    
    /** 作者名 */
    var author: String?
    
    /** 内容 */
    var content: String?
    
    /** 内容  */
    var contentAttr: NSAttributedString? {
        
        if _contentAttr == nil {
            
            let para = NSMutableParagraphStyle()
            para.lineSpacing = 5
            
            _contentAttr = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15), NSAttributedStringKey.paragraphStyle : para])
        }
        
        return _contentAttr
    }
    
    /** 内容  */
    private var _contentAttr: NSAttributedString?
    
    /** 头像 */
    var avatar: String?
    
    /** 时间 */
    var time: TimeInterval?
    
    /** 评论ID */
    var id: String?
    
    /** 喜欢数 */
    var likes: String?
    
    /** 引用的评论 */
    var reply_to: JKCommentRefModel?
    
    /****** 额外的属性 ******/
    
    /** cell行高 */
    private var _cellHeight: CGFloat = 0.0
    
    /** cell行高 */
    var cellHeight: CGFloat {
        
        if _cellHeight <= 0 {
            
            cmtRefUnfolded = true
            
            let iconW: CGFloat = 30
            let nameLabelH: CGFloat = 18
            let timeLabelH: CGFloat = 15
            let contentFont = UIFont.systemFont(ofSize: 15)
            let contentRefFont = UIFont.systemFont(ofSize: 14)
            
            let para = NSMutableParagraphStyle()
            para.lineSpacing = 5
            
            //计算文字的高度
            let maxSize = CGSize(width: JKScreenW - iconW - 28, height: CGFloat(MAXFLOAT))
            
            //评论内容高度
            var cmtContentH = ((content ?? "") as NSString).boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font : contentFont, NSAttributedStringKey.paragraphStyle : para], context: nil).size.height
            
            if cmtContentH - contentFont.lineHeight <= 5 {
                
                if jk_containChinese(str: content) {
                    
                    cmtContentH -= 5
                    
                    _contentAttr = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15)])
                }
            }
            
            contentHeight = ceil(cmtContentH)
            
            //计算cell的高度
            _cellHeight = 10 + nameLabelH + 8 + contentHeight + 8 + timeLabelH + 10;
            
            // 没有评论引用，直接返回算好的高度
            if (self.reply_to == nil) { return _cellHeight }
            
            // 评论引用已经被删除
            if (self.reply_to!.status != 0) {
                
                _cellHeight += (17 + 8)
                
                return _cellHeight
            }
            
            // 有评论引用且未被删除
            // 评论引用高度
            cmtRefHeight = self.reply_to!.cmtContentRefH
            
            cmtRefTowLinesHeight = contentRefFont.lineHeight * 2 + 5
            
            if (cmtRefHeight > 50) {
                
                cmtRefUnfolded = false
            }
            
            cellFoldHeight = _cellHeight + cmtRefTowLinesHeight + 8
            
            // 加上引用高度
            _cellHeight += cmtRefHeight + 8
        }
        
        return _cellHeight
    }
    
    /** cell折叠时行高 */
    private(set) var cellFoldHeight: CGFloat = 0
    
    /** 评论高度 */
    private(set) var contentHeight: CGFloat = 0
    
    /** 引用评论高度 */
    private(set) var cmtRefHeight: CGFloat = 0
    
    /** 引用评论两行高度 */
    private(set) var cmtRefTowLinesHeight: CGFloat = 0
    
    /** 记录引用评论是否展开 */
    var cmtRefUnfolded: Bool = true
    
    /** 记录评论是否被赞 */
    var isLiked: Bool = false
    
    /** UIMenuController */
    var menu: UIMenuController?
    
    /** UIMenuController是否已经赋值 */
    var isMenuAdded: Bool = false
}
